<?php
/**
 * 应用核心驱动
 * @author 41412754@qq.com
 */
class app {
	
	public function __construct( $group = 'www' )
	{
		session_start();
		error_reporting( E_ALL & ~ E_NOTICE );
		
		/* 变量长度限制 */
		ini_set( 'mssql.textsize', 200000 );
		ini_set( 'mssql.textlimit', 200000 );
		
		include 'base.funcs.php';
		include 'router.class.php';
		include 'control.class.php';
		include 'widget.class.php';
		include 'model.class.php';
		include 'helper.class.php';
		
		$this->group = $group;
		$this->setVars();
		$this->loadConfig();
		$this->setCharset();
		$this->setTimezone();
		$this->setDebug();
		$this->checkSafe();
	}
	
	/**
	 * 静态运行框架
	 */
	public static function run( $group = 'www' )
	{
		global $app;
		$app = new app( $group );
		$app->router = new router();
		$app->router->parseRequest();
		$app->loadModule();
	}
	
	/**
	 * 安全验证
	 */
	private function checkSafe()
	{
		/* 防止外部提交POST */
		if ( $_POST and strpos( $_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'] ) === false ) exit( '非法POST' );
		
		/* 服务器未开启自动转义时，人为给POST/GET转义 */
		if ( ! get_magic_quotes_gpc() )
		{
			if ( $this->post ) foreach ( $this->post as $key => $val )
				$this->post[$key] = mysql_real_escape_string( $val );
			
			if ( $this->get ) foreach ( $this->get as $key => $val )
				$this->get[$key] = mysql_real_escape_string( $val );
		}
	}
	
	public function setVars()
	{
		$this->post = &$_POST;
		$this->get = &$_GET;
		$this->request = &$_REQUEST;
	}
	
	/**
	 * 加载配置
	 */
	public function loadConfig()
	{
		$config = new stdClass();
		require 'config.php';
		if ( ! file_exists( $config->appConfig ) ) trigger_error( '入口配置不存在' );
		require $config->appConfig;
		
		$this->config = $config;
	}
	
	/**
	 * 设置编码
	 */
	public function setCharset()
	{
		header( 'Content-Type: text/html; charset=' . $this->config->charset );
	}
	
	/**
	 * 设置时区
	 */
	public function setTimezone()
	{
		if ( isset( $this->config->timezone ) ) date_default_timezone_set( $this->config->timezone );
	}
	
	/**
	 * 调试输出
	 */
	public function setDebug()
	{
		set_error_handler( 'customError', E_ALL & ~ E_NOTICE );
	}
	
	/**
	 * 加载类
	 * @param $className
	 * @param $static 可选
	 */
	public function loadClass( $className, $static = false )
	{
		if ( ! class_exists( $className ) )
		{
			$classFile = "{$this->config->framedir}/lib/$className/$className.class.php"; // 系统类
			if ( ! file_exists( $classFile ) )
			{
				$classFile = "../lib/$className/$className.class.php"; // 自定义类
				if ( ! file_exists( $classFile ) ) trigger_error( "类文件 $className.class.php 不存在" );
			}
			require $classFile;
		}
		
		if ( $static ) return true;
		
		if ( ! is_object( $className ) ) $this->$className = new $className();
		return $this->$className;
	}
	
	/**
	 * 连接数据库
	 * @param $dbName 可选
	 * @param $dbConfig 可选
	 */
	public function connectDB( $dbName = 'db', $config = null )
	{
		if ( $config == null ) $config = $this->config->db;
		
		if ( $config->host )
		{
			$this->$dbName = new mysql();
			$this->$dbName->connect( $config->host, $config->user, $config->pass, $config->name );
		}
		return $this->$dbName;
	}
	
	/**
	 * 加载模块
	 * @param $moduleName 可选
	 * @param $actionName 可选
	 */
	public function loadModule( $moduleName = null, $actionName = null )
	{
		$this->loadClass( 'mysql', $static = true );
		$this->connectDB();
		$this->loadClass( 'json', $static = true );
		$this->loadClass( 'validate' );
		$this->loadClass( 'front', $static = true );
		
		$moduleName = $this->module;
		$actionName = $this->action;
		
		$controlFile = "../module/$moduleName/{$moduleName}Control.class.php";
		$controlName = $moduleName . 'Control';
		if ( ! file_exists( $controlFile ) )
		{
			if ( $moduleName == $this->config->defaultModule )
			{
				$control = new control();
				$control->$actionName();
				exit();
			}
			else
			{
				trigger_error( "$controlName.class.php 不存在" );
			}
		}
		require $controlFile;
		if ( ! class_exists( $controlName ) ) trigger_error( "$controlName 类不存在" );
		if ( ! method_exists( $controlName, $actionName ) ) trigger_error( "$actionName() 方法不存在" );
		$control = new $controlName( $automated = true );
		$control->$actionName();
	}
}